/*
 * @Author: your name
 * @Date: 2020-11-04 14:07:46
 * @LastEditTime: 2021-05-05 11:46:23
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \zcz\ws-shop\src\store\index.js
 */
import Vue from 'vue'
import Vuex, { Store } from 'vuex'
import state from './state'
import mutations from './mutations'
import getters from './getters'

Vue.use(Vuex)

// 只要cart被改了，mutation被提交了 插件的代码就会执行
const cartPlugin = store => {
  store.subscribe((mutation, state) => {
    // 每当有mutation被提交，就会执行这个方法
    // 把state.cart存入localstorage
    localStorage.setItem('cart', JSON.stringify(state.cart))
    localStorage.setItem('collection', JSON.stringify(state.collection))
  })
}

export default new Store({
  plugins: [cartPlugin],
  state,
  mutations,
  getters
})
